package com.example.zbh.dsliao.thread;

import android.util.Log;

import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.view.CustomChangeDialog;

/**
 * Created by zbh on 15/12/7.
 */
public class GlobalThread implements Runnable {
    private String type;
    private String id;
    private EMGroup emGroup;
    private String content;

    public GlobalThread(String type, String id) {
        this.type = type;
        this.id = id;
    }

    public GlobalThread(String content, String id, String type) {
        this.content = content;
        this.id = id;
        this.type = type;
    }



    @Override
    public void run() {
        switch (type){
            case Constant.GROUPDETAILSGETGROUP:
                getGroup(type);
                break;
            case Constant.GROUPDETAILSCHANGE:
                changeGroupName();
                break;
            case Constant.GROUPDETAILSGROUPMESSAGE:
                hiddenGroupMessage();
                break;
            case Constant.GROUPDETAILSRELEASEGROUPMESSAGE:
                releaseGroupMessage();
                break;
            case Constant.GROUPDETAILSEXIT:
                exitGroup();
                break;
            case Constant.GROUPDETAILSREMOVE:
                removeToblack(type);
                break;
            case Constant.GROUPDETAILSHEADERREMOVE:
                clearGroupMember(type);
                break;
            case Constant.GROUPDETAILSMEMBERSEXITGROUP:
                membersExitGroup(type);
                break;


        }

    }

    //退出群聊
    public  void membersExitGroup(String type){
        try {
            EMGroupManager.getInstance().exitFromGroup(id);
            listener.showGroupResult(type, null);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }
    //删除群成员
    public void clearGroupMember(String type){
        try {
            EMGroupManager.getInstance().removeUserFromGroup(id, content);
            EMGroupManager.getInstance().createOrUpdateLocalGroup(EMGroupManager.getInstance().getGroupFromServer(id));
            emGroup = EMGroupManager.getInstance().getGroup(id);
            listener.showGroupResult(type, emGroup);


        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }

    //移动联系人到黑名单
    public void removeToblack(String type){
        try {
            EMGroupManager.getInstance().blockUser(id, content);
            EMGroupManager.getInstance().createOrUpdateLocalGroup(EMGroupManager.getInstance().getGroupFromServer(id));
            emGroup = EMGroupManager.getInstance().getGroup(id);
            listener.showGroupResult(type, emGroup);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }


    //得到群组
    public void getGroup(String type){
        try {
            emGroup = EMGroupManager.getInstance().getGroupFromServer(id);
            listener.showGroupResult(type, emGroup);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }
    //修改群名
    public void changeGroupName(){
        try {
            EMGroupManager.getInstance().changeGroupName(id, content);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }
    //屏蔽群消息
    public void hiddenGroupMessage(){
        try {
            EMGroupManager.getInstance().blockGroupMessage(id);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }

    }
    //接触屏蔽群
    public void releaseGroupMessage(){
        try {
            EMGroupManager.getInstance().unblockGroupMessage(id);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }

    }
    //退出群组
    public void exitGroup(){
        try {
            EMGroupManager.getInstance().exitAndDeleteGroup(id);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }




    //启动线程后的回调
    public interface GetGroupListener{
        void showGroupResult(String type, EMGroup emGroup);
    }
    private GetGroupListener listener;
    public void setgetGroupListener(GetGroupListener listener){
        this.listener = listener;
    }


}
